package com.six.apidemic.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.six.apidemic.R;
import com.six.apidemic.bean.LoginCallbackBean;
import com.six.apidemic.databinding.ActivityMainBinding;
import com.six.apidemic.fragment.FragmentAdapter;
import com.six.apidemic.fragment.company.CompanyFragment;
import com.six.apidemic.fragment.flow.FlowFragment;
import com.six.apidemic.fragment.main.MainFragment;
import com.six.apidemic.fragment.my.MyFragment;
import com.six.apidemic.util.LogUtils;
import com.six.apidemic.util.SpUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 主界面
 *
 * @author Administrator
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {

    ActivityMainBinding binding;
    List<Fragment> listFragments = new ArrayList<>();
    List<TextView> listTvModules = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.setActivity(this);
        initView();
        initData();
    }

    protected void initView() {
        binding.tvModuleFirstPaga.setOnClickListener(this);
        binding.tvModuleMy.setOnClickListener(this);
        listTvModules.add(binding.tvModuleFirstPaga);
        listTvModules.add(binding.tvModuleMy);
    }

    protected void initData() {
        FragmentManager fm = getSupportFragmentManager();
        listFragments.clear();
        LoginCallbackBean.ResultBean.UserBean bean = (LoginCallbackBean.ResultBean.UserBean) SpUtils.getBean(this,"module_user_bean");
        if (bean == null) {
            LogUtils.e("bean==null");
            listFragments.add(new MainFragment());
        }else{
            switch (bean.getType()){
                case "0":
                    listFragments.add(new MainFragment());
                    break;
                case "1":
                    listFragments.add(new CompanyFragment());
                    break;
                case "2":
                    listFragments.add(new FlowFragment());
                    break;
                default:
            }
        }
        listFragments.add(new MyFragment());
        FragmentAdapter fragmentAdapter = new FragmentAdapter(fm, listFragments);
        binding.vpModule.setAdapter(fragmentAdapter);
        binding.vpModule.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        setFragmentCurrent(binding.tvModuleFirstPaga,0);
                        break;
                    case 1:
                        setFragmentCurrent(binding.tvModuleMy,1);
                        break;
                    default:
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvModuleFirstPaga) {
            setFragmentCurrent(binding.tvModuleFirstPaga,0);
        } else if (id == R.id.tvModuleMy) {
            setFragmentCurrent(binding.tvModuleMy,1);
        }
    }

    /**
     * 切换fragment
     */
    public void setFragmentCurrent(TextView tvModule,int current) {
        binding.vpModule.setCurrentItem(current);
        for (TextView tv : listTvModules) {
            tv.setTextColor(getResources().getColor(R.color.black));
        }
        tvModule.setTextColor(getResources().getColor(R.color.teal_200));
    }


}